var mongoose = require('mongoose');
var async = require("async");
const DataModel = require("../models/news_model");
const Comment = require("../models/comment_model");


exports.create = function(req, res, next){
    const dataModel = new DataModel(req.body);
    // console.log(user)
    dataModel.save()
        .then(data=>{
            res.json(data);
        })
}

exports.get = function(req, res, next){
    var id = req.params.id;
    DataModel.findById(id, function (err, data) {
        res.json(data);
    })
}

exports.update = function(req, res, next){
    var id = req.params.id;
    DataModel.findByIdAndUpdate(id, {$set: req.body}, {new:false})
        .then(data=>{
            //此时的返回值是修改之前的，而非修改之后的，所以可以去MongoDB中去查看修改以后的情况
            res.json(data);
        })
}
exports.remove = function(req, res, next){
    var id = req.params.id;
    DataModel.findByIdAndRemove(id, function(err,data){
        res.json(data);
    })
}

exports.list = function(req, res, next){
    var page = (req.body.page)?req.body.page:2;
    var rows = (req.body.rows)?req.body.rows:10;

    var queryCondition = {};
    if(req.body.title && (req.body.title.trim().length)>0){
        title = req.body.title;
        queryCondition = {
            "title":new RegExp(title,"i")
        }
    }

    DataModel.paginate(queryCondition,{page:parseInt(page),limit:parseInt(rows),sort:{_id:-1}},function(err, result){
        async.map(result.docs,function(news,done){
            Comment.count({"linkid":news._id},function(err, count){
                if(err){
                    return;
                }else{
                    news.comments = count;
                    done();
                }
            })
        },function(err){
            result.rows = result.docs;
            delete result.docs;
            res.json(result);
        })


    })

}
exports.listss = function(req, res, next){
    var page = (req.body.page)?req.body.page:1;
    var limit = (req.body.limit)?req.body.limit:10;
    var ids = req.body.ids.split(",");
    var outRes = {};
    // console.log(ids);linkid:dis[i]
    //ids是一个数组
    var resNews = [];
    var flag = 0;
    for (var i in ids) {
        DataModel.find({"linkid":ids[i]},function(err, result){
            flag++;
            //result 为每一个linkid 下的news，也是一个数组
            for (var j in result) {
                resNews.push(result[j]);
            }
            if(flag==ids.length){
                outRes.total = 4;
                outRes.rows = resNews;
                outRes.page = parseInt(page);
                outRes.limit = parseInt(limit);
                outRes.pages = Math.ceil(resNews.length/limit);
                async.map(outRes.rows,function(news,done){
                    Comment.count({"linkid":news._id},function(err, count){
                        if(err){
                            return;
                        }else{
                            news.comments = count;
                            done();
                        }
                    })
                },function(err){
                    res.json(outRes);
                    // result.rows = result.docs;
                    // delete result.docs;
                    // res.json(result);
                })

                // console.log(outRes);

            }
        })

    }

}
exports.deletes = function(req, res, next){
    // console.log(req.body.ids);
    var ids = req.body.ids;
    // console.log(ids);
    if(ids.length>0){
        DataModel.remove({_id:{$in:ids.split(",")}})
            .then(data=>{
                res.json({"msg":'success',"status":200})
            })
    }else{
        res.json({"msg":'success',"status":404})
    }

}